using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos.Descriptions;
using UwinEducation.Domain.Entities;
using UwinEducation.Domain.IRepositories;
using UwinEducation.Shared.Enums;
using UwinEducation.Shared.Models;

namespace UwinEducation.Application.Services.Descriptions
{
    /// <summary>
    /// 描述管理服务实现
    /// </summary>
    public class DescriptionService : IDescriptionService
    {
        private readonly IDescriptionRepository _descriptionRepository;
        private readonly IMapper _mapper;

        public DescriptionService(
            IDescriptionRepository descriptionRepository,
            IMapper mapper)
        {
            _descriptionRepository = descriptionRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取描述详情
        /// </summary>
        public async Task<DescriptionDto> GetByIdAsync(Guid id)
        {
            var description = await _descriptionRepository.GetByIdAsync(id);
            if (description == null)
            {
                throw new ArgumentException($"描述不存在:{id}");
            }
            return _mapper.Map<DescriptionDto>(description);
        }

        /// <summary>
        /// 根据类型获取描述列表
        /// </summary>
        public async Task<List<DescriptionDto>> GetByTypeAsync(DescriptionType type)
        {
            var descriptions = await _descriptionRepository.GetByTypeAsync(type);
            return _mapper.Map<List<DescriptionDto>>(descriptions);
        }

        /// <summary>
        /// 根据标题模糊查询
        /// </summary>
        public async Task<List<DescriptionDto>> GetByTitleAsync(string title)
        {
            var descriptions = await _descriptionRepository.GetByTitleAsync(title);
            return _mapper.Map<List<DescriptionDto>>(descriptions);
        }

        /// <summary>
        /// 分页查询描述
        /// </summary>
        public async Task<PagedResult<DescriptionDto>> GetPagedAsync(DescriptionPageRequest request)
        {
            var result = await _descriptionRepository.GetPagedAsync(request);
            var dtos = _mapper.Map<List<DescriptionDto>>(result.Items);
            return new PagedResult<DescriptionDto>
            {
                Total = result.Total,
                Items = dtos
            };
        }

        /// <summary>
        /// 创建描述
        /// </summary>
        public async Task<DescriptionDto> CreateAsync(CreateDescriptionDto createDto)
        {
            // 检查标题是否存在
            var existingDescription = await _descriptionRepository.GetByTitleAsync(createDto.Title);
            if (existingDescription != null&& existingDescription.Count>0)
            {
                throw new ArgumentException($"标题已存在:{createDto.Title}");
            }

            var description = Description.Create(
                createDto.Title,
                createDto.Content,
                createDto.EnContent,
                createDto.Type
            );

            await _descriptionRepository.AddAsync(description);
            return _mapper.Map<DescriptionDto>(description);
        }

        /// <summary>
        /// 更新描述
        /// </summary>
        public async Task<DescriptionDto> UpdateAsync(UpdateDescriptionDto updateDto)
        {
            var description = await _descriptionRepository.GetByIdAsync(updateDto.Id);
            if (description == null)
            {
                throw new ArgumentException($"描述不存在:{updateDto.Id}");
            }

            description.Title = updateDto.Title ?? description.Title;
            description.Content = updateDto.Content ?? description.Content;
            description.EnContent = updateDto.EnContent ?? description.EnContent;
            description.Type = updateDto.Type ?? description.Type;

            await _descriptionRepository.UpdateAsync(description);
            return _mapper.Map<DescriptionDto>(description);
        }

        /// <summary>
        /// 删除描述
        /// </summary>
        public async Task DeleteAsync(Guid id)
        {
            var description = await _descriptionRepository.GetByIdAsync(id);
            if (description == null)
            {
                throw new ArgumentException($"描述不存在:{id}");
            }

            await _descriptionRepository.DeleteAsync(id);
        }
    }
} 